// ignore_for_file: prefer_const_constructors

import 'package:flutter/material.dart';

// ignore: import_of_legacy_library_into_null_safe
import 'package:dio/dio.dart';

Dio dio = Dio();

// 有状态控件含有数据 关键字 StatefulWidget
class BookMall extends StatefulWidget {
  const BookMall({super.key});

  @override
  bookMallState createState() {
    return bookMallState();
  }
}

// 有状态控件必须结合一个状态管理类，来进行实现
// ignore: camel_case_types
class bookMallState extends State<BookMall> {
  var books = [];

  @override
  void initState() {
    super.initState();
    getBooks();
  }

  @override
  Widget build(BuildContext context) {
    // ignore: prefer_interpolation_to_compose_strings
    return ListView.builder(
        itemCount: books.length,
        itemBuilder: ((BuildContext context, int index) {
          var item = books[index];
          return Container(
              decoration: BoxDecoration(border: Border(top: BorderSide(width: 1, color: Colors.black26))),
              child: Row(
                children: [
                  Image.network(
                    item["images"],
                    width: 130,
                    height: 180,
                    fit: BoxFit.cover,
                  ),
                  Container(
                    height: 200,
                    padding: EdgeInsets.only(left: 12),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        Text("书名：${item["bookname"]}"),
                        Text("作者：${item["author"]}"),
                        Text("书名：${item["bookname"]}"),
                        Text("书名：${item["bookname"]}"),
                      ],
                    ),
                  )
                ],
              ));
        }));
  }

  // 获取书城数据
  getBooks() async {
    // var response = await dio.get("http://47.243.245.45:8000/book");
    // var data = response.data;

    // 私有数据赋值
    setState(() {
      // books = data;
    });
    // print(data);
  }
}
